package com.rocsea.robot.service;

import com.rocsea.robot.cache.KettleDatabaseCache;
import com.rocsea.robot.dao.DatabaseAttributeDAO;
import com.rocsea.robot.dao.DatabaseDAO;
import com.rocsea.robot.exception.BusinessException;
import com.rocsea.robot.model.bo.Database;
import com.rocsea.robot.model.bo.DatabasePoint;
import com.rocsea.robot.model.entity.DatabaseAttributeDO;
import com.rocsea.robot.model.entity.DatabaseDO;
import com.rocsea.robot.utils.BeanCopyUtils;
import com.rocsea.robot.utils.KettleBuildUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;


/**
 * @Author RocSea
 * @Date 2022/12/7
 */
@Service
public class DatabaseService {

    private final DatabaseDAO databaseDAO;

    private final DatabaseAttributeDAO databaseAttributeDAO;

    public DatabaseService(DatabaseDAO databaseDAO,
                           DatabaseAttributeDAO databaseAttributeDAO) {
        this.databaseDAO = databaseDAO;
        this.databaseAttributeDAO = databaseAttributeDAO;
    }

    public DatabasePoint getPoints() {
        DatabasePoint databasePoint = new DatabasePoint();
        List<DatabaseDO> databaseList = new ArrayList<>();
        List<DatabaseAttributeDO> databaseAttributeList = new ArrayList<>();
        int moduleType = KettleBuildUtils.getModuleType();
        String databaseName = KettleBuildUtils.getSourceSchemaName();
        Database database = get(moduleType);
        String name = database.getNamePrefix() + databaseName.toUpperCase();

        Optional<DatabaseDO> one = databaseDAO.getOne(name, databaseName);
        if (!one.isPresent()) {
            Optional<Long> databaseMaxId = databaseDAO.getMaxId();
            Long idDatabase = databaseMaxId.orElse(0L);
            DatabaseDO databaseDO = BeanCopyUtils.copyProperties(database, DatabaseDO.class);
            databaseDO.setIdDatabase(++idDatabase);
            databaseDO.setName(name);
            databaseDO.setIdDatabaseType(35); // 固定值
            databaseDO.setIdDatabaseContype(1); // 固定值
            databaseDO.setDatabaseName(databaseName);
            databaseDO.setPort(-1); // 固定值
            databaseList.add(databaseDO);

            Optional<Long> databaseAttributeMaxId = databaseAttributeDAO.getMaxId();
            Long idDatabaseAttribute = databaseAttributeMaxId.orElse(0L);
            List<String> databaseAttributes = KettleDatabaseCache.getDatabaseAttributes();
            for (int i = 0; i < databaseAttributes.size(); i++) {
                DatabaseAttributeDO databaseAttributeDO = new DatabaseAttributeDO();
                databaseAttributeDO.setIdDatabaseAttribute(++idDatabaseAttribute);
                databaseAttributeDO.setIdDatabase(idDatabase.intValue());
                databaseAttributeDO.setCode(databaseAttributes.get(i));
                if (i < 4) {
                    databaseAttributeDO.setValueStr("true");
                } else if (i < 9) {
                    databaseAttributeDO.setValueStr("N");
                } else if (i < 13) {
                    databaseAttributeDO.setValueStr("Y");
                }else if (i == 13) {
                    databaseAttributeDO.setValueStr("1000");
                }else if (i == 14) {
                    databaseAttributeDO.setValueStr(database.getPort());
                }
                databaseAttributeList.add(databaseAttributeDO);
            }
            KettleBuildUtils.setIdConnection(idDatabase);
        }
        databasePoint.setDatabase(databaseList);
        databasePoint.setDatabaseAttribute(databaseAttributeList);
        return databasePoint;

    }

    public Long getIdConnectionIfNull() {
        Long idConnection = KettleBuildUtils.getIdConnection();
        String sourceSchemaName = KettleBuildUtils.getSourceSchemaName();
        int moduleType = KettleBuildUtils.getModuleType();
        String namePrefix = "";
        if (Objects.isNull(idConnection)) {
            switch (moduleType) {
                case 1:
                    namePrefix = "INTERNATIONAL_PUBLIC_MYSQL_";
                    break;
                case 2:
                    namePrefix = "DM_PUBLIC_MYSQL_";
                    break;
                case 6:
                    namePrefix = "ONSHORE_PUBLIC_MYSQL_";
                    break;
                case 41:
                    namePrefix = "THIRD_PART_PUBLIC_MYSQL_";
                    break;
            }
            Optional<DatabaseDO> one = databaseDAO.getOne(namePrefix + sourceSchemaName.toUpperCase(), sourceSchemaName);
            if (!one.isPresent()) {
                throw new BusinessException("kettle database does not exist");
            }
            idConnection = one.get().getIdDatabase();
        }
        return idConnection;
    }

    private Database get(int type) {
        Database database = new Database();
        String namePrefix = "";
        String hostName = "";
        String userName = "";
        String password = "";
        String port = "";
        switch (type) {
            case 1:
                namePrefix = "INTERNATIONAL_PUBLIC_MYSQL_";
                hostName = "${INTERNATIONAL_PUBLIC_MYSQL_HOST}";
                userName = "${INTERNATIONAL_PUBLIC_MYSQL_USER}";
                password = "${INTERNATIONAL_PUBLIC_MYSQL_PASSWORD}";
                port = "${INTERNATIONAL_PUBLIC_MYSQL_PORT}";
                break;
            case 2:
                namePrefix = "DM_PUBLIC_MYSQL_";
                hostName = "${DM_PUBLIC_MYSQL_HOST}";
                userName = "${DM_PUBLIC_MYSQL_USER}";
                password = "${DM_PUBLIC_MYSQL_PASSWORD}";
                port = "${DM_PUBLIC_MYSQL_PORT}";
                break;
            case 6:
                namePrefix = "ONSHORE_PUBLIC_MYSQL_";
                hostName = "${ONSHORE_PUBLIC_MYSQL_HOST}";
                userName = "${ONSHORE_PUBLIC_MYSQL_USER}";
                password = "${ONSHORE_PUBLIC_MYSQL_PASSWORD}";
                port = "${ONSHORE_PUBLIC_MYSQL_PORT}";
                break;
            case 41:
                namePrefix = "THIRD_PART_PUBLIC_MYSQL_";
                hostName = "${THIRD_PART_PUBLIC_MYSQL_HOST}";
                userName = "${THIRD_PART_PUBLIC_MYSQL_USER}";
                password = "${THIRD_PART_PUBLIC_MYSQL_PASSWORD}";
                port = "${THIRD_PART_PUBLIC_MYSQL_PORT}";
                break;
        }
        database.setHostName(hostName);
        database.setPassword(password);
        database.setPort(port);
        database.setUsername(userName);
        database.setNamePrefix(namePrefix);
        return database;
    }
}
